﻿using System.Collections;
using System.Collections.Generic;
using Entitas;
using UnityEngine;
using System;
using Entitas.CodeGeneration.Attributes;
using TMPro.EditorUtilities;
using UnityEngine.Networking;

public class GetSameColorSystem : ReactiveSystem<GameEntity>
{
    private Contexts _contexts;
    public GetSameColorSystem(Contexts context) : base(context.game)
    {
        _contexts = context;
    }

    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.ComponentsGetSameColor);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.isComponentsGetSameColor;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (GameEntity entity in entities)
        {
#if testCode
            Debug.Log("------------"+entity.componentsLoadPrefab.Path+"---------------");
            Debug.Log("Left Count---->"+JudgeLeft(entity).Count);
            Debug.Log("Right Count---->"+JudgeRight(entity).Count);
            Debug.Log("Up Count----->"+JudgeUp(entity).Count);
            Debug.Log("Down Count----->"+JudgeDown(entity).Count);
#endif
            entity.ReplaceComponentsDetecteSameItem(
                JudgeLeft(entity)
                ,JudgeRight(entity)
                ,JudgeUp(entity)
                ,JudgeDown(entity)
                );
   
            
            entity.isComponentsGetSameColor = false;
        }
    }


    private List<IEntity> JudgeLeft(GameEntity entity)
    {
        customData pos = entity.componentsItemIndex.index;
        List<IEntity> sameColorItems=new List<IEntity>();
        //左边颜色判断
        for (int i = pos.x - 1; i >= 0; i--)
        {
            if (!AddSameColorItem(entity, i, pos.y, sameColorItems))
            {
                break;
            }
        }

        return sameColorItems;
    }
       
    private List<IEntity> JudgeRight(GameEntity entity)
    {
        customData pos = entity.componentsItemIndex.index;
        List<IEntity> sameColorItems=new List<IEntity>();
        for (int i = pos.x + 1; i <= _contexts.game.componentsGameBoardComponents.columns - 1; i++)
        {
            if (!AddSameColorItem(entity, i, pos.y, sameColorItems))
            {
                break;
            }
        }

        return sameColorItems;
        }
        private List<IEntity> JudgeDown(GameEntity entity)
        {
            customData pos = entity.componentsItemIndex.index;
            List<IEntity> sameColorItems=new List<IEntity>();
            for (int i = pos.y - 1; i >= 0; i--)
            {
                if (!AddSameColorItem(entity, pos.x, i, sameColorItems))
                {
                    break;
                }

            }
            return sameColorItems;
        }
        private List<IEntity> JudgeUp(GameEntity entity)
        {
            customData pos = entity.componentsItemIndex.index;
            List<IEntity> sameColorItems=new List<IEntity>();
            for (int i = pos.y + 1; i <= _contexts.game.componentsGameBoardComponents.raws-1; i++)
            {
                if (!AddSameColorItem(entity, pos.x, i, sameColorItems))
                {
                    break;
                }
            }

            return sameColorItems;
        }

    private bool AddSameColorItem(GameEntity entity, int x, int y, List<IEntity> sameColorItems)
    {
        string colorName = entity.componentsLoadPrefab.Path;
        var returnValue = JudgeSameColor(colorName, x, y);
        if (!returnValue.Item1)
        {
            return false;
        }
        else
        {
            sameColorItems.Add(returnValue.Item2);
            return true;
        }
    }

    private Tuple<bool, GameEntity> JudgeSameColor(string colorName, int x, int y)
    {
        var array = _contexts.game
            .GetEntitiesWithComponentsItemIndex(new customData(x, y));
        if (array.Count == 1)
        {
            var entity = array.SingleEntity();
            if (!entity.isComponentsMovable)
            {
                return new Tuple<bool, GameEntity>(false, entity);
            }

            if (entity.componentsLoadPrefab.Path == colorName)
            {
                return new Tuple<bool, GameEntity>(true, entity);
            }
            else
            {
                return new Tuple<bool, GameEntity>(false,entity);
            }
        }
        
        return new Tuple<bool, GameEntity>(false,null);
    }
}
